#include "pch.h"
#include "seHero.h"

//const sf::Input& mInputHero = app.GetInput(); 

void seHero::Load(int number)
{
type=number-1;
frames=6;
if (number==1){
	leftKey='q';
	rightKey='w';
}else{
	leftKey='o';
	rightKey='p';
}
string tmp;
sprintf((char*)tmp.c_str(), "%d", number);
string str = tmp.c_str();
for (int a=0;a<frames;a++){
	string actual;
	sprintf((char*)actual.c_str(), "%d", a);
	string actualny = actual.c_str();
	mBackgroundLeft[a].LoadFromFile("data/hero"+str+"_left_"+actualny+".png");
	mBackgroundRight[a].LoadFromFile("data/hero"+str+"_right_"+actualny+".png");
}
mBackFront.LoadFromFile("data/hero"+str+"_"+"front.png");
//mBackRight.LoadFromFile("data/hero"+str+"_"+"right.png");
mActiveSprite.SetImage(mBackFront);
//sf::Vector2f scalevec(0.3f, 0.3f);
//mActiveSprite.SetScale(scalevec);
}

void seHero::Init()
{
	counter=0;
	iCurrentFrame = 0;
	//mActiveSprite.SetImage(mBackground[iCurrentFrame]);
	if (type==1){
		sf::Vector2f v1(viewWidth/2-100.0f,viewHeight- mActiveSprite.GetSize().y -25.0f);
		mActiveSprite.SetPosition(v1);
	}
	else{
		sf::Vector2f v1(viewWidth/2+100.0f, viewHeight- mActiveSprite.GetSize().y -25.0f);
		mActiveSprite.SetPosition(v1);
	}
}

void seHero::Step()
{

		//sf::Event event;
		//while (app.GetEvent(event)){
		//{
			//if (event.Type == sf::Event::Closed)
				//app.Close();
	sf::Vector2f actual = mActiveSprite.GetPosition();
	if (actual.x<viewWidth-905.0f){
		if (counter>0){
			inDoor=true;
			playSound("Door");
		}
		else{
			inDoor=false;
		}
		counter=0;
	}else{
		inDoor=false;
	}
	if (gInput.IsKeyDown(sf::Key::Code(leftKey))){		
		if (actual.x>viewWidth-910.0f) {
			sf::Vector2f v1(actual.x-6, actual.y);
			mActiveSprite.SetPosition(v1);
			mActiveSprite.SetImage(mBackgroundLeft[iCurrentFrame]);
			iCurrentFrame++;
			//mActiveSprite.SetImage(mBackLeft);
		}
	}
	else if (gInput.IsKeyDown(sf::Key::Code(rightKey))){
		if (actual.x<(viewWidth-mActiveSprite.GetSize().x)){
			sf::Vector2f v1(actual.x+6, actual.y);
			mActiveSprite.SetPosition(v1);		
			mActiveSprite.SetImage(mBackgroundRight[iCurrentFrame]);
			iCurrentFrame++;	
		}
		//mActiveSprite.se
	}else{
		mActiveSprite.SetImage(mBackFront);
	}
/*	else if(){
	
	}	*/	
		//}

	//iCurrentFrame++;
	if (iCurrentFrame>=frames)
		iCurrentFrame=0;
	//mActiveSprite.SetImage(mBackground[iCurrentFrame]);
}

void seHero::Draw(int number)
{
	if (type==number){
		mActiveSprite.SetColor(sf::Color( 255, 255, 255, 255));
		
		float moveX = mActiveSprite.GetSize().x*0.08f;
		float moveY = mActiveSprite.GetSize().y*0.04f;
		mActiveSprite.SetPosition(mActiveSprite.GetPosition().x-(counter+1)*moveX,mActiveSprite.GetPosition().y-(counter+1)*moveY);

		for (int a=0;a<counter;a++){
			mActiveSprite.SetScale(1+(counter-a)*0.16f,1+(counter-a)*0.08f);
			mActiveSprite.SetPosition(mActiveSprite.GetPosition().x+moveX,mActiveSprite.GetPosition().y+moveY);
			if (a==4 && counter==5)
				mActiveSprite.SetColor(sf::Color( 240, 0, 0,200));
			else
				mActiveSprite.SetColor(sf::Color( 100, 0, 0,25.0f+ a*20));
				//mActiveSprite.SetColor(sf::Color( 0, 0, 0,200));
			app.Draw(mActiveSprite);
		}
		mActiveSprite.SetPosition(mActiveSprite.GetPosition().x+moveX,mActiveSprite.GetPosition().y+moveY);
		mActiveSprite.SetScale(1.0f,1.0f);
		//mActiveSprite.SetPosition(mActiveSprite.GetPosition().x+counter*moveX,mActiveSprite.GetPosition().y+counter*moveY);
		mActiveSprite.SetColor(sf::Color( 255, 255, 255, 255));
		app.Draw(mActiveSprite);
	}else{
		//if (type==1)
		//	mActiveSprite.SetColor(sf::Color( 0x00, 0xff, 0x00, 0x80 ));
		//else
			mActiveSprite.SetColor(sf::Color( 250, 250, 210, 0x84 ));
		app.Draw(mActiveSprite);
	}
}